package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.CircleImageView;
import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.adapter.AnswerAdapter;
import cn.edu.neusoft.wuguangsheng.flowerimpression.adapter.EmptyRecyclerView;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.AnswerBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.AskBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.ListListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetAnswerListModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetAskInfoByIdModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.service.Service;

public class AnswerActivity extends BaseActivity {
    String ask_id;

    ImageView askinfo_fh;
    TextView askinfo_title,askinfo_username,askinfo_time,askinfo_content,askinfo_title1;
    CircleImageView askinfo_photo;
    View answerlist_ev;

    EmptyRecyclerView answerlist_rv;
    private List<AnswerBean> items;
    ImageView askinfo_pinglun;
    private boolean up = false;
    String otheruserid;
    TextView tv_message;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_answer);
        ask_id = getIntent().getStringExtra("ask_id");
        askinfo_fh = (ImageView)findViewById(R.id.askinfo_fh);
        askinfo_title = (TextView)findViewById(R.id.askinfo_title);
        askinfo_username = (TextView)findViewById(R.id.askinfo_username);
        askinfo_time = (TextView)findViewById(R.id.askinfo_time);
        askinfo_content = (TextView)findViewById(R.id.askinfo_content);
        askinfo_title1 = (TextView)findViewById(R.id.askinfo_title1);
        answerlist_ev = (View)findViewById(R.id.answerlist_ev);
        tv_message = (TextView)findViewById(R.id.tv_message);

        askinfo_pinglun = (ImageView)findViewById(R.id.askinfo_pinglun);

        askinfo_photo = (CircleImageView)findViewById(R.id.askinfo_photo);

        answerlist_rv = (EmptyRecyclerView)findViewById(R.id.answerlist_rv);

        //Toast.makeText(AnswerActivity.this,ask_id,Toast.LENGTH_SHORT).show();

        GetAskInfoByIdModel getAskInfoByIdModel = new GetAskInfoByIdModel();
        getAskInfoByIdModel.getAskInfoById(ask_id, new TListener<AskBean>() {
            @Override
            public void onResponse(AskBean askBean) {
                String title = askBean.getAsk_title();
                String content = askBean.getAsk_content();
                String time = askBean.getAsk_time();
                //String userimg = askBean.getUser_photo();
                String username = askBean.getUser_nickname();
                String sex = askBean.getUser_sex();
                askinfo_title.setText(title);
                askinfo_title1.setText(title);
                askinfo_content.setText(content);
                askinfo_time.setText("提问于"+time);
                askinfo_username.setText(username);
                otheruserid = askBean.getUser_id();
              //  Toast.makeText(AnswerActivity.this,askBean.getAsk_photo(),Toast.LENGTH_SHORT).show();

                if (askBean.getUser_photo().equals("1") || askBean.getUser_photo().equals("1.jpg")){
                    if (sex.equals("1")){
                        askinfo_photo.setImageResource(R.mipmap.nanshi);
                    }
                    else{
                        askinfo_photo.setImageResource(R.mipmap.nvshi);
                    }
                }
                else{
                    String img = Service.BASE_URL+"flower_impression/public/uploads/"+askBean.getUser_photo();
                    Picasso.with(AnswerActivity.this).load(img).into(askinfo_photo);
                }

            }

            @Override
            public void onFail(String msg) {

            }
        });


        GetAnswerListModel getAnswerListModel = new GetAnswerListModel();
        getAnswerListModel.getAnswerList(ask_id, new ListListener<AnswerBean>() {
            @Override
            public void onResponse(List<AnswerBean> list) {
                LinearLayoutManager linearLayoutManager=new LinearLayoutManager(AnswerActivity.this);
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                answerlist_rv.setLayoutManager(linearLayoutManager);
                items=list;
                AnswerAdapter answerAdapter=new AnswerAdapter(AnswerActivity.this,items,R.layout.answer_item);
                answerlist_rv.setAdapter(answerAdapter);
                answerlist_rv.setItemAnimator(new DefaultItemAnimator());
                tv_message.setText("还没有大神回答这个问题！");
                answerlist_rv.setEmptyView(answerlist_ev);
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    void initEvent() {
        askinfo_pinglun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AnswerActivity.this,AddAnswerInfoActivity.class);
                intent.putExtra("ask_id",ask_id);
                startActivity(intent);
            }
        });
        askinfo_fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        askinfo_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intento = new Intent(AnswerActivity.this,OtherUserInfoActivity.class);
                intento.putExtra("userid",otheruserid);
                startActivity(intento);
            }
        });


    }

    @Override
    void initDate() {
    }
    @Override
    public void onPause() {
        super.onPause();
        up = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (up) {
            initView();
            initEvent();
            initDate();
            up = false;
        }
    }
}
